<template>
  <div class="md-user-address-info">
    <div class="r-title">
      <span>Delivery Address</span>
      <b class="add" @click="handleAdd">add</b>
    </div>
    <div class="user-info-main">
      <div class="main-box">
        <el-table :data="tableData">
          <el-table-column prop="name" label="name" min-width="120" fixed="left"></el-table-column>
          <el-table-column prop="phone" label="contact number" min-width="140"></el-table-column>
          <el-table-column prop="email" label="email" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column prop="zipCode" label="ZIP Code" min-width="120"></el-table-column>
          <el-table-column prop="country" label="Country" min-width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.country && scope.row.country.split(',')[1] }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="state" label="State" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column prop="address" label="address" min-width="180" show-overflow-tooltip></el-table-column>
          <el-table-column prop="operate" label="operate" min-width="130" center>
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope.row)">edit</el-button>
              <el-button type="text" @click="handleDelete(scope.row)">delete</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="isDefault" label="isDefault" min-width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.isDefault == '0'">yes</span>
              <span v-if="scope.row.isDefault == '1'">no</span>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="status" min-width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.status == '0'">Normal</span>
              <span v-if="scope.row.status == '1'">Disable</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <md-address-dialog :flag="flag" :rowInfo="rowInfo" @close="flag = false" @refresh="getAddressList"></md-address-dialog>
  </div>
</template>
<script>
export default {
  name: 'MDUserBaseInfo',
  layout: 'dynamic',
  data () {
    return {
      tableData: [],
      flag: false,
      rowInfo: {}
    }
  },
  mounted () {
    this.getAddressList()
  },
  methods: {
    async getAddressList () {
      const options = {
        userId: this.$local.getItem('user').userId,
        pageIndex: 1,
        pageSize: 100
      }
      const res = await this.$axios.post('web/address/listPage', options)
      if (res.code == 200) {
        this.tableData = res.data.items
      }
    },
    handleAdd () {
      this.flag = true
      this.rowInfo = {}
    },
    async handleEdit (row) {
      this.flag = true
      this.rowInfo = row
    },
    handleDelete (row) {
      this.$confirm('This will permanently delete the address. Continue?', 'Warning', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(async () => {
        const res = await this.$axios.delete(`web/address/${row.addressId}`)
        if (res.code == 200) {
          this.$message.success('delete address success!')
          this.getAddressList()
        }
      }).catch(() => { })
    },
  }
}
</script>
<style lang="scss" scoped>
.md-user-address-info {
  width: 100%;
  .r-title {
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #ededed;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #414141;
    padding-right: 20px;
    box-sizing: border-box;
    b {
      cursor: pointer;
      color: $md-primary-color;
    }
    span {
      font-weight: 100;
      font-size: 24px;
      line-height: 40px;
      border-bottom: 1px solid $md-primary-color;
    }
  }
  .user-info-main {
    margin-top: 10px;
  }
}
</style>